﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EShopV20.Controllers
{
    public class ProductController : Controller
    {
        EShopV20DbContext db = new EShopV20DbContext();

        [Authorize]
        public ActionResult MyProducts()
        {
            var model = db.OrderDetails
                .Where(d => d.Order.CustomerId == User.Identity.Name)
                .Select(d => d.Product)
                .Distinct();
            return View("List", model);
        }

        public ActionResult ListByCategory(int Id)
        {
            var model = db.Products.Where(p => p.CategoryId == Id);
            return View("List", model);
        }

        public ActionResult ListBySupplier(String Id)
        {
            var model = db.Products.Where(p => p.SupplierId == Id);
            return View("List", model);
        }

        public ActionResult ListByName(String Keywords)
        {
            var model = db.Products
                .Where(p => p.Name.Contains(Keywords));
            return View("List", model);
        }

        public ActionResult ListBySpecial(String Id)
        {
            List<Product> model = null;
            switch (Id)
            {
                case "BAN_CHAY":
                    model = db.Products
                        .Where(p => p.OrderDetails.Count > 0)
                        .OrderByDescending(p => p.OrderDetails.Sum(d => d.UnitPrice * d.Quantity))
                        .Take(15).ToList();
                    break;
                case "MOI":
                    model = db.Products
                        .Where(p => p.Latest == true).ToList();
                    break;
                case "GIAM_GIA":
                    model = db.Products
                        .Where(p => p.Discount > 0)
                        .OrderByDescending(p=>p.Discount)
                        .ToList();
                    break;
                case "YEU_THICH":
                    model = db.Products.ToList();
                    break;
                case "XEM_NHIEU":
                    model = db.Products
                        .Where(p => p.Views > 0)
                        .OrderByDescending(p => p.Views)
                        .ToList();
                    break;
            }
            
            return View("List", model);
        }

        public ActionResult Detail(int Id)
        {
            var model = db.Products.Find(Id);

            // Tăng số lần xem
            model.Views++;
            db.SaveChanges();

            //Ghi nhận hàng đã xem
            var views = Request.Cookies["Views"];
            if (views == null) {
                views = new HttpCookie("Views", Id.ToString());
            }
            if (!views.Value.Contains(Id.ToString()))
            {
                views.Value += "," + Id;
            }
            views.Expires = DateTime.Now.AddMonths(1);
            Response.Cookies.Add(views);

            // Truy vấn các mặt hàng đã xem
            var ids = views.Value.Split(',')
                .Select(id => int.Parse(id)).ToList();
            ViewBag.Views = db.Products.Where(p => ids.Contains(p.Id));

            return View(model);
        }
	}
}